package com.tinyideatech.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyideatech.common.json.JSONObject;
import com.tinyideatech.common.utils.DateUtils;
import com.tinyideatech.system.entity.MallOrder;
import com.tinyideatech.system.mapper.CarOrderMapper;
import com.tinyideatech.system.mapper.MallOrderMapper;
import com.tinyideatech.system.service.IMallOrderService;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author zhangy-5
 * @date 2019-08-26
 */
@Service
public class MallOrderServiceImpl extends ServiceImpl<MallOrderMapper, MallOrder> implements IMallOrderService {

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private MallOrderMapper mallOrderMapper;

    @Autowired
    private CarOrderMapper carOrderMapper;

    @Override
    public void changeStatus(MallOrder mallOrder) {

        MallOrder order = getById(1L);
        if (order != null) {
            order.setStatus(mallOrder.getStatus());
            updateById(order);
        }
    }

    @Override
    public JSONObject consumeAll() {
        JSONObject result = new JSONObject();
        double adas = 0.0;
        double model = 0.0;
        Integer carConsume = carOrderMapper.consume();
        Double mallConsume = mallOrderMapper.consume();
        if (carConsume != null && mallConsume != null) {
            adas = Fen2Yuan(carConsume);
            model = mallConsume;
        }
        result.put("adas", adas);
        result.put("model", model);
        result.put("total", adas + model);
        return result;
    }

    @Override
    public JSONObject select() {
        // 按周统计总收入
        Calendar c = Calendar.getInstance();
        int weeks = c.get(Calendar.WEEK_OF_MONTH);
        double singleADAS;
        double singleModel;
        double singleSum;
        List<Double> adas = new ArrayList<>();
        List<Double> model = new ArrayList<>();
        List<Double> sum = new ArrayList<>();
        while (weeks > 0) {
            Integer carOrder = carOrderMapper.sumByMonth(c.getTime());
            Double mallOrder = mallOrderMapper.sumByMonth(c.getTime());
            if (carOrder == null) {
                singleADAS = 0;
                adas.add(singleADAS);
            } else {
                singleADAS = Fen2Yuan(carOrderMapper.sumByMonth(c.getTime()));
                adas.add(singleADAS);
            }
            if (mallOrder == null) {
                singleModel = 0;
                model.add(singleModel);
            } else {
                singleModel = mallOrderMapper.sumByMonth(c.getTime());
                model.add(singleModel);
            }
            singleSum = singleADAS + singleModel;
            sum.add(singleSum);
//            day = getPastDate(7);
            c = getPastDateByWeek(c);
            weeks--;
        }
        JSONObject result = new JSONObject();
        Collections.reverse(adas);
        Collections.reverse(model);
        Collections.reverse(sum);
        result.put("adas", adas);
        result.put("model", model);
        result.put("sum", sum);
        return result;
    }

    @Override
    public JSONObject increase() {
        // 每周统计增长量
        Calendar c = Calendar.getInstance();
        int weeks = c.get(Calendar.WEEK_OF_MONTH);
        List<Integer> adas = new ArrayList<>();
        List<Integer> model = new ArrayList<>();
        while (weeks > 0) {
            int modelPlus = mallOrderMapper.singlePlus(c.getTime());
            int adasPlus = carOrderMapper.singlePlus(c.getTime());
            adas.add(adasPlus);
            model.add(modelPlus);
            c = getPastDateByWeek(c);
            weeks--;
        }
        JSONObject result = new JSONObject();
        Collections.reverse(adas);
        Collections.reverse(model);
        result.put("adas", adas);
        result.put("model", model);
        return result;
    }

    @Override
    public List<List<Integer>> sum() {
        List<Integer> adas = new ArrayList();
        List<Integer> model = new ArrayList();
        List<List<Integer>> object = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        for (int i = 1; i < 13; i++) {
            adas.add(carOrderMapper.countByMonth(year, i));
            model.add(mallOrderMapper.countByMonth(year, i));
        }
        object.add(adas);
        object.add(model);
        return object;
    }

    @Override
    public JSONObject countWeek() {
        List<Integer> adas = new ArrayList();
        List<Integer> model = new ArrayList();
        JSONObject object = new JSONObject();
        for (int i = 0; i < 7; i++) {
            adas.add(carOrderMapper.countByToday(getPastDate(i)));
            model.add(mallOrderMapper.countByToday(getPastDate(i)));
        }
        object.put("adas", adas);
        object.put("model", model);
        return object;
    }

    @Override
    public XSSFWorkbook downloadRecorder() {
        List<MallOrder> mallOrders = mallOrderMapper.selectList(null);
        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("mall_order");
        Row titleRow = sheet.createRow(0);
        titleRow.createCell(1).setCellValue("订单编号");
        titleRow.createCell(2).setCellValue("用户名称");
        titleRow.createCell(3).setCellValue("总价格");
        titleRow.createCell(4).setCellValue("支付时间");
        titleRow.createCell(5).setCellValue("设备编号");
        titleRow.createCell(6).setCellValue("支付结果");
        int cell = 1;
        for (MallOrder mallOrder : mallOrders) {
            Row row = sheet.createRow(cell);
            row.createCell(0).setCellValue(cell);
            row.createCell(1).setCellValue(mallOrder.getOrderId());
            row.createCell(2).setCellValue(mallOrder.getUsername());
            row.createCell(3).setCellValue(mallOrder.getTotalPrice());
            row.createCell(4).setCellValue(DateUtils.parseDateToStr("yyyy/MM/dd", mallOrder.getPaymentTime()));
            row.createCell(5).setCellValue(mallOrder.getDeviceId());
            if (mallOrder.getPaymentResult().equals("1")) {
                row.createCell(6).setCellValue("成功");
            } else {
                row.createCell(6).setCellValue("失败");
            }
            cell++;
        }
        return wb;
    }


    /**
     * 元转分
     *
     * @param yuan
     * @return
     */
    private static Integer Yuan2Fen(Double yuan) {
        return new BigDecimal(String.valueOf(yuan)).movePointRight(2).intValue();
    }

    /**
     * 分转元
     *
     * @param fen
     * @return
     */
    private static Double Fen2Yuan(Integer fen) {
        return new BigDecimal(fen).movePointLeft(2).doubleValue();
    }

    private static Date getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        return calendar.getTime();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    }


    private static Calendar getPastDateByWeek(Calendar calendar) {
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7);
        return calendar;
    }

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        int weeks = c.get(Calendar.WEEK_OF_MONTH);
        while (weeks > 0) {
            System.out.println(c.getTime());
            c = getPastDateByWeek(c);
            weeks--;
        }

    }
}
